package com.campus.client.controller;

import com.campus.client.net.ClientConnector;
import com.campus.common.message.AdminCourseRequest;
import com.campus.common.message.AdminCourseResponse;
import com.campus.common.vo.Classroom;
import com.campus.common.vo.Course;
import com.campus.common.vo.Student;
import com.campus.common.vo.Teacher;

import java.io.IOException;
import java.util.List;

public class AdminCourseController {
    private ClientConnector connector;

    public AdminCourseController(ClientConnector connector) {
        this.connector = connector;
    }

    // 修改课程信息
    public String updateCourseInfo(String courseId, String newName, int newCredits, String newDepartment)
            throws IOException, ClassNotFoundException {
        AdminCourseRequest req = new AdminCourseRequest();
        req.setAction(AdminCourseRequest.Action.UPDATE_COURSE);
        req.setCourseId(courseId);
        req.setCourseName(newName);
        req.setCredit(newCredits);
        req.setDepartment(newDepartment);   // ✅ 新增// ✅ 新增
        return connector.sendAdminCourseRequest(req).getMessage();
    }


    // 增加班级
    public String addClassroom(Classroom classroom) throws IOException, ClassNotFoundException {
        AdminCourseRequest req = new AdminCourseRequest();
        req.setAction(AdminCourseRequest.Action.ADD_CLASSROOM);
        req.setClassroom(classroom);
        return connector.sendAdminCourseRequest(req).getMessage();
    }

    // 删除班级
    public String deleteClassroom(String classId) throws IOException, ClassNotFoundException {
        AdminCourseRequest req = new AdminCourseRequest();
        req.setAction(AdminCourseRequest.Action.DELETE_CLASSROOM);
        req.setClassId(classId);
        return connector.sendAdminCourseRequest(req).getMessage();
    }

    // 修改班级信息
    public String updateClassroom(Classroom classroom) throws IOException, ClassNotFoundException {
        AdminCourseRequest req = new AdminCourseRequest();
        req.setAction(AdminCourseRequest.Action.UPDATE_CLASSROOM);
        req.setClassroom(classroom);
        return connector.sendAdminCourseRequest(req).getMessage();
    }

    // 给班级添加学生
    public String addStudentToClass(String classId, String studentId) throws IOException, ClassNotFoundException {
        AdminCourseRequest req = new AdminCourseRequest();
        req.setAction(AdminCourseRequest.Action.ADD_STUDENT_TO_CLASS);
        req.setClassId(classId);
        req.setStudentId(studentId);
        return connector.sendAdminCourseRequest(req).getMessage();
    }

    // 从班级移除学生
    public String removeStudentFromClass(String classId, String studentId) throws IOException, ClassNotFoundException {
        AdminCourseRequest req = new AdminCourseRequest();
        req.setAction(AdminCourseRequest.Action.REMOVE_STUDENT_FROM_CLASS);
        req.setClassId(classId);
        req.setStudentId(studentId);
        return connector.sendAdminCourseRequest(req).getMessage();
    }

    // 查询课程
    public List<Course> queryCourses(String keyword) throws IOException, ClassNotFoundException {
        AdminCourseRequest req = new AdminCourseRequest();
        req.setAction(AdminCourseRequest.Action.QUERY_COURSE);
        req.setKeyword(keyword);
        return connector.sendAdminCourseRequest(req).getCourses();
    }

    // 查看所有课程
    public List<Course> viewAllCourses() throws IOException, ClassNotFoundException {
        AdminCourseRequest req = new AdminCourseRequest();
        req.setAction(AdminCourseRequest.Action.VIEW_ALL_COURSES);
        return connector.sendAdminCourseRequest(req).getCourses();
    }

    // 查看某个班级的学生名单
    public List<Student> viewStudentsInClass(String classId) throws IOException, ClassNotFoundException {
        AdminCourseRequest req = new AdminCourseRequest();
        req.setAction(AdminCourseRequest.Action.VIEW_STUDENTS_IN_CLASS);
        req.setClassId(classId);
        AdminCourseResponse resp = connector.sendAdminCourseRequest(req);
        return resp.getStudents(); // 学生对象列表
    }

    // ✅ 查看所有教师
    public List<Teacher> viewAllTeachers() throws IOException, ClassNotFoundException {
        AdminCourseRequest req = new AdminCourseRequest();
        req.setAction(AdminCourseRequest.Action.VIEW_ALL_TEACHERS);
        AdminCourseResponse resp = connector.sendAdminCourseRequest(req);
        return resp.getTeachers(); // 教师对象列表
    }
    // AdminCourseController.java
    public String addCourse(String courseId, String courseName, int credit, String department)
            throws IOException, ClassNotFoundException {
        AdminCourseRequest req = new AdminCourseRequest();
        req.setAction(AdminCourseRequest.Action.ADD_COURSE);
        req.setCourseId(courseId);        // ✅ 直接设置
        req.setCourseName(courseName);    // ✅ 直接设置
        req.setCredit(credit);
        req.setDepartment(department);    // ✅ 新增字段
        return connector.sendAdminCourseRequest(req).getMessage();
    }
    // 删除课程
    public String deleteCourse(String courseId) throws IOException, ClassNotFoundException {
        AdminCourseRequest req = new AdminCourseRequest();
        req.setAction(AdminCourseRequest.Action.DELETE_COURSE);
        req.setCourseId(courseId);
        return connector.sendAdminCourseRequest(req).getMessage();
    }



    // 关闭连接
    public void closeConnection() throws IOException {
        connector.close();
    }
}
